package com.example.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedQueueConfig {
    //延迟交换机名称
    private static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    //队列
    private static final String DELAYED_QUEUE_NAME = "delayed.queue";
    //routingKey
    private static final String DELAYED_ROUTING_KEY = "delayed.routingKey";

    //声明延迟交换机 - 自定义交换机
    @Bean("customExchange")
    public CustomExchange customExchange() {
        /*
         * 1.交换机名称
         * 2.交换机类型
         * 3.是否需要持久化
         * 4.是否需要自动删除
         * 5.参数
         */
        Map<String, Object> arguments = new HashMap<>(1);
        arguments.put("x-delayed-type", "direct");//延迟类型
        return new CustomExchange(DELAYED_EXCHANGE_NAME, "x-delayed-message",
                false, false, arguments);
    }

    //声明队列
    @Bean("queue")
    public Queue queue() {
        return new Queue(DELAYED_QUEUE_NAME);
    }

    //绑定
    @Bean
    public Binding delayedQueueBindingDelayedExchange() {
        return BindingBuilder.bind(queue()).to(customExchange()).with(DELAYED_ROUTING_KEY).noargs();
    }

}
